#include "global.h"
#include "input.h"

CPlayerInput::CPlayerInput()
{
	for (int iPlayer = 0; iPlayer < 4; iPlayer++)
	{
		for (int iKey = 0; iKey < NUM_KEYS; iKey++)
		{
			outputControls[iPlayer].keys[iKey].fPressed = false;
			outputControls[iPlayer].keys[iKey].fDown = false;
		}
	}
	
	iPressedKey = 0;
	fUsingMouse = false;
}

void CPlayerInput::CheckIfMouseUsed()
{
#ifndef _XBOX
	for (short iGameState = 0; iGameState < 2; iGameState++)
	{
		for (short iPlayer = 0; iPlayer < 4; iPlayer++)
		{
			CInputControl* inputControl = &inputControls[iPlayer]->inputGameControls[iGameState];
			
			for (short iKey = 0; iKey < NUM_KEYS; iKey++)
			{
				short iInputKey = inputControl->keys[iKey];
				
				if ((iInputKey >= MOUSE_UP && iInputKey <= MOUSE_RIGHT) ||
					iInputKey == MOUSE_BUTTON_START + 4 || iInputKey == MOUSE_BUTTON_START + 5)
				{
					fUsingMouse = true;
					return;
				}
			}
		}
	}
#endif
	fUsingMouse = false;
}

void CPlayerInput::ClearPressedKeys(short iGameState)
{
	for (int iPlayer = 0; iPlayer < 4; iPlayer++)
	{
		CInputControl* inputControl = &inputControls[iPlayer]->inputGameControls[iGameState];
		COutputControl* outputControl = &outputControls[iPlayer];
		
		for (int iKey = 0; iKey < NUM_KEYS; iKey++)
		{
			outputControl->keys[iKey].fPressed = false;
			
#ifndef _XBOX
			if (fUsingMouse)
			{
				short iInputKey = inputControl->keys[iKey];
				
				if ((iInputKey >= MOUSE_UP && iInputKey <= MOUSE_RIGHT))
				{
					outputControl->keys[iKey].fDown = false;
				}
				
				if (iInputKey == MOUSE_BUTTON_START + 4 || iInputKey == MOUSE_BUTTON_START + 5)
				{
					outputControl->keys[iKey].fDown = false;
				}
			}
#endif
		}
	}
	
	iPressedKey = 0;
}

void CPlayerInput::ResetKeys()
{
	for (int iPlayer = 0; iPlayer < 4; iPlayer++)
	{
		for (int iKey = 0; iKey < NUM_KEYS; iKey++)
		{
			outputControls[iPlayer].keys[iKey].fPressed = false;
			outputControls[iPlayer].keys[iKey].fDown = false;
		}
	}
	
	iPressedKey = 0;
}

void CPlayerInput::Update(SDL_Event event, short iGameState)
{
	bool bFound = false;
	for (short iPlayer = -1; iPlayer < 4; iPlayer++)
	{
		CInputControl* inputControl;
		COutputControl* outputControl;
		short iPlayerID = iPlayer;
		short iDeviceID = DEVICE_KEYBOARD;
		
		if (iPlayer == -1)
		{
			if (iGameState == 1 && inputControls[0]->iDevice != DEVICE_KEYBOARD)
			{
				inputControl = &game_values.inputConfiguration[0][0].inputGameControls[1];
				outputControl = &outputControls[0];
				iPlayerID = 0;
				iDevice = game_values.inputConfiguration[0][0].iDevice;
			}
			else
			{
				continue;
			}
		}
		else
		{
			if (!inputControls[iPlayer])
				continue;
				
			inputControl = &inputControls[iPlayer]->inputGameControl[iGameState];
			outputControl = &outputControls[iPlayer];
			iDeviceID = inputControls[iPlayer]->iDevice;
		}
		
		if (iDeviceID == DEVICE_KEYBOARD)
		{
			if (SDL_KEYDOWN == event.type)
			{
				for (int iKey = 0; iKey < NUM_KEYS && !fFound; iKey++)
				{
					if (inputControl->keys[iKey] == event.key.keysym.sym)
					{
						fFound = true;
						
						if (iGameState == 0 && game_values.playercontrol[iPlayer] != 1 && iKey < 6)
							continue;
							
						if (!outputControl->keys[iKey].fDown)
							outputControl->keys[iKey].fPressed = true;
							
						outputControl->keys[iKey].fDown = true;
					}
				}
				
				iPressedKey = (short)event.key.keysym.sym;
			}
			else if (SDL_KEYUP == event.type)
			{
				for (int iKey = 0; iKey < NUM_KEYS && !fFound; iKey++)
				{
					if (inputControl->keys[iKey] == event.key.keysym.sym)
					{
						fFound = true;
						
						if (iGameState == 0 && game_values.playercontrol[iPlayer] != 1 && iKey < 6)
							continue;
							
						outputControl->keys[iKey].fDown = false;
					}
				}
			}
			else if (SDL_MOUSEMOTION == event.type)
			{
				for (int iKey = 0; iKey < NUM_KEYS && !fFound; iKey++)
				{
					if (inputControl->keys[iKey] >= MOUSE_UP)
					{
						if ((inputControl->keys[iKey] == MOUSE_UP && event.motion.yrel < -MOUSE_Y_DEAD_ZONE) ||
							(inputControl->keys[iKey] == MOUSE_DOWN && event.motion.yrel > MOUSE_Y_DEAD_ZONE) ||
							(inputControl->keys[iKey] == MOUSE_LEFT && event.motion.xrel < -MOUSE_X_DEAD_ZONE) ||
							(inputControl->keys[iKey] == MOUSE_RIGHT && event.motion.xrel > MOUSE_X_DEAD_ZONE) ||
							(inputControl->keys[iKey] >= MOUSE_BUTTON_START && (event.motion.state & SDL_BUTTON(inputControl->keys[iKey] - MOUSE_BUTTON_START))))
						{
							fFound = true;
							
							if (iGameState == 0 && game_values.playercontrol[iPlayer] != 1 && iKey < 6)
								continue;
								
							if (!outputControl->keys[iKey].fDown)
								outputControl->keys[iKey].fPressed = true;
								
							outputControl->keys[iKey].fDown = true;
						}
						else
						{
							if (iGameState == 0 && game_values.playercontrol[iPlayer] != 1 && iKey < 6)
								continue;
								
							if (inputControl->keys[iKey] == MOUSE_BUTTON_START + 4 || inputControl->keys[iKey] == MOUSE_BUTTON_START + 5)
								continue;
								
							outputControl->keys[iKey].fDown = false;
						}
					}
				}
			}
			else if (SDL_MOUSEBUTTONDOWN == event.type)
			{
				for (int iKey = 0; iKey < NUM_KEYS && !fFound; iKey++)
				{
					if (inputControl->keys[iKey] == event.button.button + MOUSE_BUTTON_START)
					{
						fFound = true;
						
						if (iGameState == 0 && game_values.playercontrol[iPlayer] != 1 && iKey < 6)
							continue;
							
						if (!outputControl->keys[iKey].fDown)
							outputControl->keys[iKey].fPressed = true;
							
						outputControl->keys[iKey].fDown = true;
					}
				}
			}
			else if (SDL_MOUSEBUTTONUP == event.type)
			{
				for (int iKey = 0; iKey < NUM_KEYS && !fFound; iKey++)
				{
					if (inputControl->keys[iKey] == event.button.button + MOUSE_BUTTON_START)
					{
						fFound = true;
						
						if(inputControl->keys[iKey] == MOUSE_BUTTON_START + 4 || inputControl->keys[iKey] == MOUSE_BUTTON_START + 5)
							continue;
							
						if (iGameState == 0 && game_values.playercontrol[iPlayer] != 1 && iKey < 6)
							continue;
							
						outputControl->keys[iKey].fDown = false;
					}
				}
			}
		}
		else
		{
			if (SDL_JOYHATMOTION == event.type)
			{
				if (iDeviceID != event.jhat.which)
					continue;
					
				for (int iKey = 0; iKey < NUM_KEYS; iKey++)
				{
					if (inputControl->keys[iKey] >= JOY_HAT_UP && inputControl->keys[iKey] <= JOY_HAT_RIGHT)
					{
						if ((inputControl->keys[iKey] == JOY_HAT_UP && (event.jhat.value & SDL_HAT_UP)) ||
							(inputControl->keys[iKey] == JOY_HAT_DOWN && (event.jhat.value & SDL_HAT_DOWN)) ||
							(inputControl->keys[iKey] == JOY_HAT_LEFT && (event.jhat.value & SDL_HAT_LEFT)) ||
							(inputControl->keys[iKey] == JOY_HAT_RIGHT && (event.jhat.value & SDL_HAT_RIGHT)))
						{
							fFound = true;
							
							if (iGameState == 0 && game_values.playercontrol[iPlayer] != 1 && iKey < 6)
								continue;
								
							if (!outputControl->keys[iKey].fDown)
								outputControl->keys[iKey].fPressed = true;
								
							outputControl->keys[iKey].fDown = true;
						}
						else
						{
							if (iGameState == 0 && game_values.playercontrols[iPlayer] != 1 && iKey < 6)
								continue;
								
							outputControl->keys[iKey].fDown = false;
						}
					}
				}
			}
			else if (SDL_JOYBUTTONDOWN == event.type)
			{
				if (iDeviceID != event.jbutton.which)
					continue;
				
				for (int iKey = 0; iKey < NUM_KEYS && !fFound; iKey++)
				{
					if (inputControl->keys[iKey] == event.jbutton.button + JOY_BUTTON_START)
					{
						fFound = true;
						
						if (iGameState == 0 && game_values.playercontrols[iPlayer] != 1 && iKey < 6)
							continue;
							
						if (!outputControl->keys[iKey].fDown)
							outputControl->keys[iKey].fPressed = true;
							
						outputControl->keys[iKey].fDown = true;
					}
				}
			}
			else if (SDL_JOYBUTTONUP == event.type)
			{
				if (iDeviceID != event.jbutton.which)
					continue;
					
				for (int iKey = 0; iKey < NUM_KEYS && !fFound; iKey++)
				{
					if (inputControl->keys[iKey] == event.jbutton.button + JOY_BUTTON_START)
					{
						fFound = true;
						
						if (iGameState == 0 && game_values.playercontrol[iPlayer] != 1 && iKey < 6)
							continue;
							
						outputControl->keys[iKey].fDown = false;
					}
				}
			}
			else if (SDL_JOYAXISMOTION == event.type)
			{
				if (iDeviceID != event.jaxis.which)
					continue;
					
				for (int iKey = 0; iKey < NUM_KEYS; iKey++)
				{
					bool fUseJoystickInput = false;
					bool fJoystickDown = false;
					
					if (event.jaxis.axis == 0 && inputControl->keys[iKey] == JOY_STICK_1_LEFT)
					{
						fUseJoystickInput = true;
						
						if (event.jaxis.value < -JOYSTICK_DEAD_ZONE)
							fJoystickDown = true;
					}
					else if (event.jaxis.axis == 0 && inputControl->keys[iKey] == JOY_STICK_1_RIGHT)
					{
						fUseJoystickInput = true;

						if(event.jaxis.value > JOYSTICK_DEAD_ZONE)
							fJoystickDown = true;
					}
					else if (event.jaxis.axis == 1 && inputControl->keys[iKey] == JOY_STICK_1_UP)
					{
						fUseJoystickInput = true;
						
						if (event.jaxis.value < -JOYSTICK_DEAD_ZONE)
							fJoystickDown = true;
					}
					else if (event.jaxis.axis == 1 && inputControl->keys[iKey] == JOY_STICK_1_DOWN)
					{
						fUseJoystickInput = true;
						
						if (event.jaxis.value > JOYSTICK_DEAD_ZONE)
							fJoystickDown = true;
					}
					else if (event.jaxis.axis == 2 && inputControl->keys[iKey] == JOY_STICK_2_LEFT)
					{
						fUseJoystickInput = true;
						
						if (event.jaxis.value < -JOYSTICK_DEAD_ZONE)
							fJoystickDown = true;
					}
					else if (event.jaxis.axis == 2 && inputControl->keys[iKey] == JOY_STICK_2_RIGHT)
					{
						fUseJoystickInput = true;
						
						if (event.jaxis.value > JOYSTICK_DEAD_ZONE)
							fJoystickDown = true;
					}
					else if (event.jaxis.axis == 3 && inputControl->keys[iKey] == JOY_STICK_2_UP)
					{
						fUseJoystickInput = true;
						
						if (event.jaxis.value < -JOYSTICK_DEAD_ZONE)
							fJoystickDown = true;
					}
					else if (event.jaxis.axis == 3 && inputControl->keys[iKey] == JOY_STICK_2_DOWN)
					{
						fUseJoystickInput = true;
						
						if (event.jaxis.value > JOYSTICK_DEAD_ZONE)
							fJoystickDown = true;
					}
					
					if (fUseJoystickInput)
					{
						if (iGameState == 0 && game_values.playercontrol[iPlayer] != 1 && iKey < 6)
							continue;
							
						if (fJoystickDown)
						{
							if (!outputControl->keys[iKey].fDown)
								outputControl->keys[iKey].fPressed = true;
								
							outputControl->keys[iKey].fDown = true;
						}
						else
						{
							outputControl->keys[iKey].fDown = false;
						}
					}
				}
			}
		}
	}
}
